package com.qk.controller;

import com.qk.common.Result;
import com.qk.service.UserService;
import com.qk.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @Autowired
    private UserService userService;


    @PostMapping("/upload")
        public Result upload(MultipartFile image) throws Exception {
            log.info("文件上传: {}",image);
            String fileName = image.getOriginalFilename() ;//获取原始文件名
            String extName =  fileName.substring(fileName.lastIndexOf(".")); //获取文件后缀
            String newFileName = UUID.randomUUID().toString() + extName;//构造新的文件名
            //2.将文件保存到服务器中
           String url = aliyunOSSOperator.upload(image.getBytes(), newFileName);
            return Result.success(url);
        }



}
